//
//  TestYogaCellVC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/6/3.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import CooperSwift
import YogaKit
import RxSwift

class TestYogaCellVC: UIViewController {

    private let disposeBag = DisposeBag()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        bgColor(.white)
        
        // 1
        let contentView = UIView()
        contentView.backgroundColor = .lightGray
        // 2
        contentView.configureLayout { (layout) in
          // 3
          layout.isEnabled = true
          // 4
          layout.flexDirection = .row
          layout.width = YGValue(Screen.width - 20)
          layout.height = 80
            layout.marginTop = YGValue(Screen.statusBarHeight + Screen.navBarHeight + 10)
          layout.marginLeft = 10
            layout.alignItems = .center
        }
        
        contentView.addSubviews(arroundViews())

        
        let centerV = UIView.init().then {
            $0.backgroundColor = .random()
        }
        centerV.configureLayout {
            $0.isEnabled = true
            $0.flexGrow = 1
            $0.marginLeft = 4
            $0.height = 44
        }
        contentView.addSubview(centerV)
        
//        let tf = UITextField.init().then {
//            $0.placeholder = "请输入..."
//        }
//        tf.configureLayout {
//            $0.isEnabled = true
//            $0.flexGrow = 1
//        }
//        centerV.addSubview(tf)
//
        
        let bt = UIButton.init().then {
            $0.setTitle("点击", for: .normal)
        }
        bt.rx.tap.subscribe(onNext: {
            
            guard let disappearingView = contentView.subviews.first else { return }
            disappearingView.yoga.isIncludedInLayout = !disappearingView.yoga.isIncludedInLayout
            disappearingView.isHidden = !disappearingView.isHidden

            contentView.configureLayout { (layout) in
              // 3
              layout.isEnabled = true
              // 4
              layout.flexDirection = .row
              layout.width = YGValue(Screen.width - 20)
              layout.height = 80
                layout.marginTop = 0
              layout.marginLeft = 0
                layout.alignItems = .center
            }
            contentView.yoga.applyLayout(preservingOrigin: true)
            
        }).disposed(by: disposeBag)
        bt.configureLayout {
            $0.isEnabled = true
            $0.flexGrow = 1
        }
        centerV.addSubview(bt)
        
//        contentView.addSubviews(arroundViews())
        
    
        view.addSubview(contentView)
        contentView.yoga.applyLayout(preservingOrigin: true)
        
    
    }
    
    func arroundViews() -> [UIView] {
        let leftLB1 = UILabel.init().then {
            $0.text = "LB1"
            $0.textColor = .blue
        }
        leftLB1.configureLayout {
            $0.isEnabled = true
            $0.marginLeft = 4
        }
        
        let leftImg1 = UIImageView.init().then {
            $0.image = UIImage.init(named: "settings")
        }
        leftImg1.configureLayout {
            $0.isEnabled = true
            $0.marginLeft = 4
            $0.width = 22
            $0.height = 22
        }
        
        let leftLB2 = UILabel().then {
            $0.text = "LB2"
            $0.textColor = .blue
        }
        leftLB2.configureLayout {
            $0.isEnabled = true
            $0.marginLeft = 4
        }
        
        let leftImg2 = UIImageView.init().then {
            $0.image = UIImage.init(named: "settings")
        }
        leftImg2.configureLayout {
            $0.isEnabled = true
            $0.marginLeft = 4
            $0.width = 22
            $0.height = 22
        }
        return [leftImg1, leftLB1, leftImg2, leftLB2]
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
